import logging
from logging.handlers import RotatingFileHandler
from logging import Logger, Handler, Formatter


class Loggers:

    @staticmethod
    def configurate_loggers(logger_obj: Logger,
                            handler: Handler,
                            logger_level: int = 20,
                            is_propagate: bool = True) -> str:
        """
        配置一个日志记录器

        :param logger_obj:
        :param handler:
        :param logger_level:
        :param is_propagate:
        :return:
        """

        if logger_level == 0:
            logger_obj.setLevel(logging.NOTSET)
        elif logger_level == 10:
            logger_obj.setLevel(logging.DEBUG)
        elif logger_level == 20:
            logger_obj.setLevel(logging.INFO)
        elif logger_level == 30:
            logger_obj.setLevel(logging.WARNING)
        elif logger_level == 40:
            logger_obj.setLevel(logging.ERROR)
        else:
            logger_obj.setLevel(logging.CRITICAL)

        logger_obj.addHandler(handler)

        if not is_propagate:
            logger_obj.propagate = False

        return "successful"

    @staticmethod
    def generate_loggers(logger_name: str,
                         handler: Handler,
                         logger_level: int = 20,
                         is_propagate: bool = True) -> Logger:
        """
        生成一个日志记录器

        :param logger_name:
        :param handler:
        :param logger_level:
        :param is_propagate:
        :return:
        """

        logger_ = logging.getLogger(logger_name)

        if logger_level == 0:
            logger_.setLevel(logging.NOTSET)
        elif logger_level == 10:
            logger_.setLevel(logging.DEBUG)
        elif logger_level == 20:
            logger_.setLevel(logging.INFO)
        elif logger_level == 30:
            logger_.setLevel(logging.WARNING)
        elif logger_level == 40:
            logger_.setLevel(logging.ERROR)
        else:
            logger_.setLevel(logging.CRITICAL)

        logger_.addHandler(handler)

        if not is_propagate:
            logger_.propagate = False

        return logger_


    @staticmethod
    def generate_handler(log_store_path: str,
                         formatter: Formatter,
                         max_bytes: int = 1000*1000,
                         backup_count: int = 5,
                         handlers_level: int = 20) -> Handler:
        """
        生成并配置一个日志处理器，该日志处理器实现的是RotatingFileHandler

        :param log_store_path:
        :param formatter:
        :param max_bytes:
        :param backup_count:
        :param handlers_level:
        :return:
        """

        rfh_handlers = RotatingFileHandler(filename=log_store_path,
                                           maxBytes=max_bytes,
                                           backupCount=backup_count,
                                           encoding="utf-8")

        if handlers_level == 0:
            rfh_handlers.setLevel(logging.NOTSET)
        elif handlers_level == 10:
            rfh_handlers.setLevel(logging.DEBUG)
        elif handlers_level == 20:
            rfh_handlers.setLevel(logging.INFO)
        elif handlers_level == 30:
            rfh_handlers.setLevel(logging.WARNING)
        elif handlers_level == 40:
            rfh_handlers.setLevel(logging.ERROR)
        else:
            rfh_handlers.setLevel(logging.CRITICAL)

        rfh_handlers.setFormatter(formatter)

        return rfh_handlers

    @staticmethod
    def generate_formatter(formatter_style: str = "") -> Formatter:
        """
        生成并配置一个格式化器

        :param formatter_style:
        :return:
        """

        if formatter_style == "":
            formatter_style = '%(asctime)s--%(name)s--%(levelname)s--%(message)s'
        formatter_ = Formatter(formatter_style)
        return formatter_








